package com.pro.api;
import java.util.ArrayList;
import	java.util.HashMap;

import com.pro.VO.CourseDetailsVO;
import com.pro.VO.IndexCourseJXVO;
import com.pro.VO.IndexCourseTJVO;
import com.pro.bean.CourseAVInfo;
import com.pro.bean.Courseinfo;
import com.pro.dto.Dto;
import com.pro.service.CourseAVInfoService;
import com.pro.service.CourseinfoService;
import com.pro.service.UserInfoService;
import com.pro.util.dtoUtil.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/***
 *
 *Cousreinfo表的控制层
 */
@RestController
@RequestMapping("cousreinfo")
public class CousreinfoController {
    /***
     * 注入CouserinfoSevice服务层，对它的方法进行调用
     */
    @Autowired
    private CourseinfoService courseinfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private CourseAVInfoService courseAVInfoService;


    @GetMapping("selAPG")
    public Dto selAPGoData(){
        return DtoUtil.returnSuccess(courseinfoService.selAPG());
    }

    //最新or最热
    @GetMapping("keyword")
    public Dto keyword(String keyword){
            if(keyword.equals("new")){
                return DtoUtil.returnSuccess(courseinfoService.newest());
            }else{
                return DtoUtil.returnSuccess(courseinfoService.hot());
            }
    }

    /***
     * 查询推荐课程显示到首页
     */
    @GetMapping("indexRecommend")
    public Dto indexRecommend(int offset){
        Map map =new HashMap();
        map.put("offset",offset);

        List<Courseinfo> courseinfos = courseinfoService.indexRecommend(map);
        List<IndexCourseTJVO> indexCourseTJVOS = new ArrayList<> ();
        for (int i = 0; i < courseinfos.size(); i++) {//将数据库取出的数据循环放入到VO视图对象中
            Courseinfo courseinfo = courseinfos.get(i);
            IndexCourseTJVO add = new IndexCourseTJVO();
            add.setId(courseinfo.getId());
            add.setCoursedescription(courseinfo.getCoursedescription());
            add.setCoursename(courseinfo.getCoursename());
            add.setCourseprice(courseinfo.getCourseprice());
            add.setCourseteacher(courseinfo.getCourseteacher());
            add.setLearnnum(courseinfo.getLearnnum());
            add.setTotalhour(courseinfo.getTotalhour());
            add.setType(courseinfo.getType());
            add.setCourseimgurl(courseinfo.getCourseimgurl());
            indexCourseTJVOS.add(add);
        }
        return DtoUtil.returnSuccess(indexCourseTJVOS);
    }

    /***
     * 查询二级分类推荐课程显示到二级分类页
     */
    @GetMapping("indexGoods")
    public Dto indexGoods(int offset,int categoryLevel2){
        Map map = new HashMap();
        map.put("offset",offset);
        map.put("categoryLevel2",categoryLevel2);

        List<Courseinfo> courseinfos = courseinfoService.indexRecommend(map);
        List<IndexCourseTJVO> indexCourseTJVOS = new ArrayList<> ();
        for (int i = 0; i < courseinfos.size(); i++){//封装视图对象
            Courseinfo courseinfo = courseinfos.get(i);
            IndexCourseTJVO add = new IndexCourseTJVO();
            add.setId(courseinfo.getId());
            add.setCourseimgurl(courseinfo.getCourseimgurl());
            add.setTotalhour(courseinfo.getTotalhour());
            add.setLearnnum(courseinfo.getLearnnum());
            add.setCoursename(courseinfo.getCoursename());
            indexCourseTJVOS.add(add);
        }
        return DtoUtil.returnSuccess(indexCourseTJVOS);
    }

    /***
     * 查询二级分类首页精选课程
     */
    @GetMapping("indexSelection")
    public Dto indexSelection(int categoryLevel2){
       Map map = new HashMap();
       map.put("categoryLevel2",categoryLevel2);
       List<Courseinfo> courseinfos = courseinfoService.indexSelection(map);
       List<IndexCourseJXVO> indexCourseTJVOS = new ArrayList<>();
       for (int i = 0; i < courseinfos.size(); i++){
           Courseinfo courseinfo = courseinfos.get(i);
           IndexCourseJXVO add = new IndexCourseJXVO();
           add.setId(courseinfo.getId());
           add.setCoursename(courseinfo.getCoursename());
           add.setCourseimgurl(courseinfo.getCourseimgurl());
           add.setCoursedescription(courseinfo.getCoursedescription());
           add.setCourseteacher(courseinfo.getCourseteacher());
           indexCourseTJVOS.add(add);
       }
       return DtoUtil.returnSuccess(indexCourseTJVOS);
    }

    /***
     * 查询根据课程id相关的课程信息及讲师信息，影音信息
     */
    @GetMapping("CourseDetails")
    public Dto CourseDetails(int courseId,int userId){
        //课程信息及教师相关信息
        Courseinfo courseinfo = courseinfoService.selCourseById(courseId);
        //课程目录相关影音信息
        List<CourseAVInfo> courseAVInfo = courseAVInfoService.selA_VInfoById(courseId);
        //查询当前用户的VIP信息
        int vipStatus = userInfoService.selVipStatus(userId);
        //封装课程详情VO视图
        CourseDetailsVO courseDetailsVO = new CourseDetailsVO();
        courseDetailsVO.setCourseinfo(courseinfo);
        courseDetailsVO.setCourseAVInfo(courseAVInfo);
        courseDetailsVO.setVipStatus(vipStatus);

        return DtoUtil.returnSuccess(courseDetailsVO);
    }
}
